package org.swan.web.user.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.stream.base.MessageBean;
import org.swan.web.user.bean.UserBean;
import org.swan.web.user.service.UserService;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/get")
    public MessageBean<UserBean> get(@RequestParam("userId") long userId) {
        return MessageBean.MSG_SUCCESS.setData(userService.getUserById(userId));
    }

    @PostMapping("add")
    public MessageBean<UserBean> add(@RequestBody UserBean userBean) {
        long userId = userService.addUser(userBean);
        return MessageBean.MSG_SUCCESS.setData(userService.getUserById(userBean.getUserId()));
    }

    @PostMapping("update")
    public MessageBean<UserBean> update(@RequestBody UserBean userBean) {
        return MessageBean.MSG_SUCCESS.setData(userService.updateUser(userBean));
    }

    @GetMapping("delete")
    public MessageBean<UserBean> delete(@RequestParam("userId") long userId) {
        return MessageBean.MSG_SUCCESS.setData(userService.deleteUserById(userId));
    }

    @GetMapping("list")
    public MessageBean<List<UserBean>> list(@Param("offset") int offset, @Param("rows") int rows) {
        return MessageBean.MSG_SUCCESS.setData(userService.list(offset, rows));
    }
}
